<template>
  <div class="container">
    <div class="body-main">
      <div>
        <el-form ref="rolesForm" :inline="true" :model="roles" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input v-model="roles.name" placeholder="角色名称" />
          </el-form-item>
          <div style="float: right; margin-right:none; margin-top: 6px;">
            <el-button type="primary" size="mini" @click="search">搜索</el-button>
            <el-button plain size="mini" @click="reset()">重置</el-button>
          </div>
        </el-form>
        <div style="position: relative;">
          <div style="width: 50%; position: relative; left: 0;height: 28px;line-height: 28px;">{{ this.$route.meta.title }}</div>
          <div style="width: 50%; position: relative; right: 0; text-align: right;float: right; top: -28px;">
            <el-button type="primary" size="mini" @click="add">添加</el-button>
            <el-button plain size="mini" @click="refresh">刷新</el-button>
          </div>
        </div>

        <el-table
          v-loading="listLoading"
          :data="list"
          :border="true"
          style="width: 100%"
        >
          <el-table-column
            fixed
            prop="name"
            label="名称"
          />
          <el-table-column
            prop="created_at"
            label="创建时间"
            width="200"
          />
          <el-table-column
            prop="updated_at"
            label="更新时间"
            width="200"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="235"
          >
            <template slot-scope="scope">
              <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
              <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
              <el-button type="primary" size="mini" @click="info(scope.row)">角色信息</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="laypage.total > 0"
          :total="laypage.total"
          :page.sync="laypage.number"
          :limit.sync="laypage.size"
          @pagination="getData"
        />
      </div>
    </div>

    <el-dialog
      title="添加角色"
      :visible.sync="showRoleForm"
      width="30%"
    >
      <el-form ref="rolesForm" :model="rolesData">
        <el-form-item label="角色名称">
          <el-input v-model="rolesData.name" placeholder="角色名称" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showRoleForm = false">取 消</el-button>
        <el-button type="primary" @click="roleComfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { req } from '@/api/request'
import Pagination from '@/components/Pagination'

export default {
  name: 'Roles',
  components: { Pagination },
  data() {
    return {
      roles: {
        name: ''
      },
      list: [],
      params: {},
      laypage: {
        number: 1,
        size: 10,
        total: 0
      },
      listLoading: true,
      rolesData: {},
      showRoleForm: false,
      operation: '',
      current_row: {}
    }
  },
  created() {
    console.log('roles')
    this.getData()
  },
  mounted() {
    //
  },
  methods: {
    // 获取列表数据
    getData() {
      this.listLoading = true
      req('admin/roles', 'GET', this.params).then(response => {
        // console.log('请求返回', response)
        this.list = response.data.data
        this.laypage.total = response.data.total
        this.laypage.number = response.data.current_page
        this.listLoading = false
      })
        .catch(error => {
          console.log('错误', error)
          this.listLoading = false
        })
    },
    // 搜索
    search() {
      console.log('点击了搜索按钮')
      console.log('表单数据：', this.roles)
      // filter[name]
      this.params = {
        'filter[name]': this.roles.name
      }
      this.getData()
    },

    // 重置搜索
    reset(formName) {
      console.log('点击了重置按钮')
      this.roles = {
        name: ''
      }
      this.params = {}
      this.getData()
    },

    // 添加
    add() {
      // console.log('点击了添加按钮')
      this.rolesData = {}
      this.showRoleForm = true
      this.operation = 'add'
    },
    // 请求接口添加
    roleComfirm() {
      // console.log('角色名称：', this.roles)
      if (this.operation === 'add') {
        req('admin/roles', 'POST', this.rolesData).then(response => {
          // console.log('请求返回', response)
          this.showRoleForm = false
          this.refresh()
        })
      }
      if (this.operation === 'edit') {
        req('admin/roles/' + this.current_row.id, 'PUT', this.rolesData).then(response => {
          // console.log('请求返回', response)
          this.showRoleForm = false
          this.refresh()
        })
      }
    },

    // 刷新
    refresh() {
      console.log('点击了刷新按钮')
      this.params = {}
      this.getData()
    },

    // 删除
    del(row) {
      console.log('点击了删除按钮', row)
    },

    // 编辑
    edit(row) {
      console.log('点击了编辑按钮', row)
      this.operation = 'edit'
      this.current_row = row
      this.showRoleForm = true
      this.rolesData = row
    },

    // 信息
    info(row) {
      console.log('点击了角色信息按钮', row)
    }
  }
}
</script>

<style lang="scss">
  .body-main{
    width: calc(100% - 30px);
    margin-left: 15px;
    margin-top: 15px;
    background-color: #FFFFFF;
    padding: 15px;
  }
</style>
